package widget.utils;

import android.support.v4.view.ViewCompat;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by Mr.Fan on 2016-6-13.
 */
 public class TouchUtils {
    public static final class MoveListener {
        private float TOUCH_DOWN_X;
        private float TOUCH_DOWN_Y;

        private float TOUCH_MOVE_X;
        private float TOUCH_MOVE_Y;

        public void onTouch(MotionEvent ev) {
            int action = ev.getAction();
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    TOUCH_DOWN_X = ev.getX();
                    TOUCH_DOWN_Y = ev.getY();
                case MotionEvent.ACTION_MOVE:
                    TOUCH_MOVE_X = ev.getX();
                    TOUCH_MOVE_Y = ev.getY();
            }
        }

        public boolean isMoveHorizontal() {
            float dx = Math.abs(TOUCH_DOWN_X - TOUCH_MOVE_X);
            float dy = Math.abs(TOUCH_DOWN_Y - TOUCH_MOVE_Y);
            if (dy < dx) {
                return true;
            }
            return false;
        }

        public boolean isMoveVertical() {
            float dx = Math.abs(TOUCH_DOWN_X - TOUCH_MOVE_X);
            float dy = Math.abs(TOUCH_DOWN_Y - TOUCH_MOVE_Y);
            if (dy > dx) {
                return true;
            }
            return false;
        }

        public boolean isMoveUp() {
            float dy = TOUCH_MOVE_Y - TOUCH_DOWN_Y;
            if (dy < 0 && isMoveVertical()) {
                return true;
            }
            return false;
        }

        public boolean isMoveDown() {
            float dy = TOUCH_MOVE_Y - TOUCH_DOWN_Y;
            if (dy > 0 && isMoveVertical()) {
                return true;
            }
            return false;
        }

        public boolean isMoveLeft() {
            float dx = TOUCH_MOVE_X - TOUCH_DOWN_X;
            if (dx < 0 && isMoveHorizontal()) {
                return true;
            }
            return false;
        }

        public boolean isMoveRight() {
            float dx = TOUCH_MOVE_X - TOUCH_DOWN_X;
            if (dx > 0 && isMoveHorizontal()) {
                return true;
            }
            return false;
        }

    }

    public static View.OnTouchListener AlphaOnTouch(final View.OnTouchListener onTouchListener) {
        return new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        ViewCompat.setAlpha(v, 0.75f);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_OUTSIDE:
                    case MotionEvent.ACTION_CANCEL:
                        ViewCompat.setAlpha(v, 1.0f);
                        break;
                }
                return onTouchListener != null ? onTouchListener.onTouch(v, event) : false;
            }
        };
    }


}
